<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Config;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class ConfigController extends Controller
{
//    将数据库的config表中的网站配置数据写入config目录下的webconfig文件
    public function putFile()
    {
//        1 从config表中取出网站配置数组
         $conf =  Config::lists('conf_content','conf_name')->toArray();
        // dd($conf);
//         将数组变量变成字符串格式的数组
       $arr = '<?php return  '.var_export($conf,true).';';

//       2 将数据写入config目录下的webconfig文件
//        参数一 要写入的文件的路径
//        参数二 要写入的文件的内容
        file_put_contents(base_path().'/config/webconfig.php',$arr);
    }
//    批量更新内容
    public function changeContent(Request $request)
    {
        $input = $request->only('conf_id','conf_content');
//        dd($input);
        $conf_id = $input['conf_id'];
        $conf_content = $input['conf_content'];
        foreach ($conf_id as $k=>$v) {
//            通过$v作为id，获取到要修改那那条记录
           $conf =  Config::find($v);
           $conf->conf_content = $input['conf_content'][$k];
           $conf->save();
        }
        $this->putFile();
        return redirect('admin/config');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //判断field_type字段，来决定conf_content的显示样式

        $data = Config::orderBy('conf_order','asc')->get();
        foreach($data as $k=>$v){
            switch ($v['field_type']){
                case 'input':
//                    <input type="text" name="conf_name">
                 $v['conf_contents'] = '<input class="lg" type="text" name="conf_content[]" value="'.$v['conf_content'].'">';

                    break;
                case "textarea":
//                    <textarea name="conf_tips"></textarea>
                    $v['conf_contents'] = '<textarea name="conf_content[]">'.$v['conf_content'].'</textarea>';
                    break;

                case 'radio':
//                    1|开启,0|关闭
//                <input type="radio" name="conf_content[]" value="1">开启　
//                <input type="radio" name="conf_content[]" value="0">关闭　
                   $a = explode(',',$v['field_value']);
//                    $a = [
//                        0=>'1|开启',
//                        1=>'0|关闭'
//                    ]
                    $str = '';
                    foreach ($a as $m=>$n){
//                        $n = '1|开启';
//                        $n = [
//                            0=>1,
//                            1=>'开启'
//                        ]
                        $aa = explode('|',$n);
//                        <input type="radio" name="conf_content[]" value="1">开启　
//                        判断此单选按钮是否被选中
                        $c = ($aa[0] == $data[$k]['conf_content'])?'checked':'';
                      $str.=   '<input  type="radio" name="conf_content[]" '.$c.' value="'.$aa[0].'">'.$aa[1];
                    }
                    $v['conf_contents'] = $str;
                    break;
            }
        }
        return view('admin.config.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.config.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
       $input =  $request->except('_token');
//       dd($input);
       $conf =  Config::create($input);
       if($conf){
           $this->putFile();
           return redirect('admin/config');
       }else{
           return back()->with('errors','添加失败');
       }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
